package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.CheckItem;
import com.itxiaowu.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组体检检查管理
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
     private CheckItemService checkItemService;

    /**
     * 查询分页数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/queryPage.do")
//    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean){

        return checkItemService.queryPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),
                queryPageBean.getQueryString());

    }

    /**
     * 根据id值获得检查项数据
     */
    @RequestMapping("/queryById.do")
//    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result queryById(Integer id){
            CheckItem checkItem = null;
        try{
            checkItem = checkItemService.queryById(id);
        }catch(Exception e){
            e.printStackTrace();
            /*查询失败*/
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        /*查询成功*/
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);

    }
    /**
     * 编辑检查项
     */
    @RequestMapping("/editcheckitem.do")
//    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result editCheckItem(@RequestBody CheckItem checkItem){

        try{
            checkItemService.editCheckItem(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            /*编辑失败*/
            new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }

        /*编辑成功*/
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);

    }

    /**
     * 添加检查项
     */
    @RequestMapping("/addcheckitem.do")
//    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result addCheckItem(@RequestBody CheckItem checkItem){

        try{
            checkItemService.addCheckItem(checkItem);
        }catch(Exception e){
            //添加失败返回失败信息
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        /*添加成功返回成功信息*/
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    /**
     * 删除检查项
     */
//    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deletecheckitem.do")
    public Result deleteCheckItem(Integer id){

        try{
            checkItemService.deleteCheckItem(id);
        }catch (Exception e){
            e.printStackTrace();
            //删除失败
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //删除成功
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);


    }

    /**
     * 获得全部检查项
     */
    @RequestMapping("/findAll.do")
//    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){

        try{
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch(Exception e){

            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }


}
